﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using Microsoft.Surface.Presentation.Manipulations;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace MapSurface.MapSurfaceGUI
{
    public class UIElementMoveHandler
    {
        Contact uielementControlContact;
        Point lastPoint;
        public UIElement MainCanvas;
        public bool moveElement = false;
        public SurfaceWindow1 window;
        public UIElementMoveHandler(SurfaceWindow1 wind)
        {
            window = wind;
            MainCanvas = wind.DesignSpace;
        }

        public void Drag_Button_MouseDown(object sender, ContactEventArgs e)
        {
            // Capture to the ellipse.  
            e.Contact.Capture((UIElement)sender);

            // Remember this contact if a contact has not been remembered already.  
            // This contact is then used to move the ellipse around.
            if (uielementControlContact == null)
            {
                uielementControlContact = e.Contact;

                // Remember where this contact took place.  
                lastPoint = uielementControlContact.GetPosition(MainCanvas);
                window.selectUIElement(sender);
            }

            // Mark this event as handled.  
            e.Handled = true;


        }

        public void Drag_Button_MouseMove(object sender, ContactEventArgs e)
        {
            if (e.Contact == uielementControlContact)
            {
                // Get the current position of the contact.  
                Point currentTouchPoint = uielementControlContact.GetCenterPosition(MainCanvas);

                // Get the change between the controlling contact point and
                // the changed contact point.  
                double deltaX = currentTouchPoint.X - lastPoint.X;
                double deltaY = currentTouchPoint.Y - lastPoint.Y;

                // Get and then set a new top position and a new left position for the ellipse.  
                double newTop = Canvas.GetTop((UIElement)sender) + deltaY;
                double newLeft = Canvas.GetLeft((UIElement)sender) + deltaX;

                Canvas.SetTop((UIElement)sender, newTop);
                Canvas.SetLeft((UIElement)sender, newLeft);

                // Forget the old contact point, and remember the new contact point.  
                lastPoint = currentTouchPoint;

                // Mark this event as handled.  
                e.Handled = true;
            }

        }

        public void Drag_Button_MouseLeave(object sender, ContactEventArgs e)
        {
            // If this contact is the one that was remembered  
            if (e.Contact == uielementControlContact)
            {
                // Forget about this contact.
                uielementControlContact = null;
            }

            // Mark this event as handled.  
            e.Handled = true;

        }

 
    }
}
